﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module.Commons;
using System.IO;

/// <summary>
/// 课件下载
/// </summary>
public partial class main_course_DownloadCourse :   UserControl.CommonPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string cId = Util.objectToString(Page.Request.QueryString["CSW_ID"]);
        if (cId != "")
        {
            cmdDownload(cId);
        }
    }

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void cmdDownload(string id)
    {

        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        //当前时间
        string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");

        List<CDEEntity> listEntity = new List<CDEEntity>();

        CDEEntityManager courseEm = CDEEMFactory.getEMByFullName("WEB.CSW_MANAGE");
        CDESearchCriteria courseSc = new CDESearchCriteria(courseEm);
        courseSc.addCondition("ID", id);
        List<CDEEntity> courseList = CDEService.LoadEntities(courseEm, courseSc);

        if (courseList.Count > 0)
        {
            CDEEntity m_entity = courseList[0];
            string path = System.Configuration.ConfigurationSettings.AppSettings["Courseware"];
            string file = System.Web.HttpContext.Current.Server.MapPath(path + m_entity.get("CSW_ID").ToString().Trim());
            System.IO.FileInfo myfile = new System.IO.FileInfo(file);
            if (myfile.Exists)
            {

                //下载次数
                string downLoads = Util.objectToNumbString(int.Parse(Util.objectToNumbString(m_entity.get("DOWNLOADS"))) + 1);
                m_entity.set("DOWNLOADS", downLoads);
                m_entity.set("UPDATE_USER", op.SysID);
                m_entity.set("UPDATE_DATE", Now);
                m_entity.Operation = CDEDataOperation.UPDATE;
                listEntity.Add(m_entity);
                CDEService.UpdateEntities(courseEm, listEntity);

                try
                {
                        long fileSize = myfile.Length; 
                        Response.Clear();
                        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(m_entity.get("FILE_PATH").ToString().Trim()));
                        Response.AddHeader("Content-Length", fileSize.ToString());
                        Response.ContentType = "application/octet-stream";
                        Response.WriteFile(file, 0, fileSize); 
                        Response.Flush(); 
                        Response.Close();

                }
                 catch (Exception ex)
                {
                    Response.Write("Error:" + ex.Message);
                }
                finally
                {                  
                    Response.Close();
                }
            }
            else
            {
                showMessageErrAndOk("文件未找到，请联系管理员!");
            }
        }
    }     
 }
